home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / tcbeep.arc / BEEP.C next >
Text File  |  1988-03-24  |  3KB  |  76 lines

  1. /*************************************************************************
  2. * Turbo Pascal includes nice built in 'sound' and 'nosound' functions.   *
  3. * I used these functions  in all of my programs to produce a pleasant    *
  4. * 'diddle-diddle' sound when a user makes an incorrect data entry. I     *
  5. * find this much more pleasant than the ASCII 7 'beeeep' from the PC's   *
  6. * speaker.  Unfortunately, Turbo C does not have any built in speaker    *
  7. * control (at least none that I have found yet) and all of the public    *
  8. * domain speaker controls are pretty difficult to implement for such a   *
  9. * trivial application ('diddle-diddle').  The following code ('beep')    *
  10. * requires only 20 lines and allows you to produce a speaker tone for    *
  11. * essentially any length of time.  Please refer to Peter Norton's ex-    *
  12. * cellent book ("Programmer's Guide to the IBM-PC, page 148 to 152 for   *
  13. * a lucid explanation and examples in BASICA.  The 'main' given here     *
  14. * a very short and stupid demo.  To get a 'diddle-diddle' sound, try:    *
  15. *                  beep( 1000, 10000 );                                  *
  16. *                  beep(  600, 5000  );                                  *
  17. * I hope you find this routine useful... -  mike groh, Lexington, MA     *
  18. **************************************************************************/
  19.  
  20.  
  21. int beep( freq, duration )
  22. int freq;      /* frequency to play */
  23. int duration;  /* how long (clock ticks) to sound */
  24. {
  25.   int count;
  26.   int old, new;
  27.  
  28.   count = ( 1193280 / freq );
  29.  
  30.                                /* the 8253 timer is on port 67 */
  31.   outport( 67, 182 );          /* get timer ready       */
  32.   outport( 66, count % 256 );  /* load low order byte   */
  33.   outport( 66, count / 256 );  /* load high order byte  */
  34.  
  35.   old = inport( 97 );          /* speaker is on port 97 */
  36.   new = ( old | 3 );
  37.   outport( 97, new );          /* start speaker         */
  38.   while( count++ < duration )  /* delay while speak makes tone */
  39.     ;
  40.   outport( 97, old );          /* stop speaker          */
  41.   return( 0 );
  42. }
  43.  
  44.  
  45.  
  46. void main()  /* a quickie demo... */
  47. {
  48.    int i;
  49.  
  50.    beep(   200, 10000 );
  51.    for( i = 0; i <= 2000; i++ );
  52.    beep(   400, 10000 );
  53.    for( i = 0; i <= 2000; i++ );
  54.    beep(   600, 10000 );
  55.    for( i = 0; i <= 2000; i++ );
  56.    beep(   800, 10000 );
  57.    for( i = 0; i <= 2000; i++ );
  58.    beep(  1000, 50 );
  59.    for( i = 0; i <= 2000; i++ );
  60.    beep(  2000, 10000 );
  61.    for( i = 0; i <= 2000; i++ );
  62.    beep(  3000, 10000 );
  63.    for( i = 0; i <= 2000; i++ );
  64.    beep(  4000, 10000 );
  65.    for( i = 0; i <= 2000; i++ );
  66.    beep(  5000, 10000 );
  67.    for( i = 0; i <= 2000; i++ );
  68.    beep(  6000, 10000 );
  69.    for( i = 0; i <= 2000; i++ );
  70. }
  71.  
  72. /* OOPS - I see my TurboC 1.5 does include a 'sound()' and 'nosound()'!
  73. *  Oh well - at least the above code works without having to include
  74. *  "conio.h" in the source code.  In any case, if you're working with 1.0
  75. *  this code my still be useful...
  76. */